package com.guo.anim.util;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

public class XmlParseUtil {
    public static void xmlParse(String layoutXml){
        System.out.println("find layout xml file"+layoutXml);
        /**修改xml中的相应的对象*/
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try{
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(layoutXml);
            Element nodeList = document.getDocumentElement();
            Attr attr = nodeList.getAttributeNode("android:background");
            if(null != attr){
                System.out.println("nodeList tag = "+nodeList.getTagName()+",length = "+nodeList.getAttributeNode("android:background").getValue());
            }
            if(nodeList.getChildNodes().getLength() > 0){
                for(int i = 0; i < nodeList.getChildNodes().getLength(); i++){
                    Node node = nodeList.getChildNodes().item(i);
                    System.out.println("node attr =" + node.getAttributes()+",tag = ");
                    if(node instanceof NodeList){
                        System.out.println("node list");
                    }
                }
            }
        }catch (ParserConfigurationException p){
            System.out.println("parse config exception");
        }catch (SAXException s){
            System.out.println("sax exception");
        }catch (IOException e){
            System.out.println("ioe exception");
        }

    }
}
